import 'dart:developer';

import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/constant/to_order_detail_source.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/model/order_list_entity.dart';
import 'package:guochaoapp/model/usersuid_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCMineController extends GetxController {
  UserService userService;

  EasyRefreshController er_controller = EasyRefreshController();
  int page = 1;
  int size = 0;
  var isEmpty = true.obs;
  var records = RxList<OrderListDataRecord>.empty();

  var account = "用户名称".obs;
  var phone = "".obs;
  var balance = 0.0.obs;

  clearUserDara() {
    account.value = "用户名称";
    phone.value = "";
    balance.value = 0.0;
    size = 0;
    isEmpty.value = true;
    records.assignAll([]);
    records.refresh();
  }

  reqeustMinePageData() {
    requestUserInfo();
    er_controller.callRefresh();
  }

  requestUserInfo() {
    userService = Get.find();
    if (userService.isLogin) {
      HttpClient.request<UsersuidEntity>(
          url: UserApi.usersUid + userService.userInfo.uid,
          method: Method.GET,
          onSuccess: (_, model) async {
            account.value = model.data.account;
            phone.value = model.data.phone;
            balance.value = model.data.balance;
          });
    }
  }

  settingTap() {
    userService = Get.find();
    if (userService.isLogin) {
      Get.toNamed(Routes.SETTING);
    } else {
      userService.removeUserInfo();
      Get.toNamed(Routes.LOGIN, arguments: "mine");
    }
  }

  detailTap() {}

  Future<void> onRefresh(bool finish) async {
    await requestOrderList(false, finish);
  }

  Future<void> onLoadMore(bool finish) async {
    await requestOrderList(true, finish);
  }

  //isload yes上拉 false下啦
  requestOrderList([bool isload = false, bool finish]) {
    userService = Get.find();

    if(userService.isLogin == false) {
      er_controller.finishRefresh();
      return;
    }

    if (isload) {
      page++;
    } else {
      page = 1;
    }

    HttpClient.request<OrderListEntity>(
      url: MallApi.orderList,
      method: Method.GET,
      params: {
        "uid": userService.userInfo.uid,
        "finish": finish,
        "current": page,
        "size": 5
      },
      onSuccess: (_, model) async {
        Future.delayed(Duration(milliseconds: 800)).then((e) {
          if (isload) {
            bool noMore = false;
            if (model.data.records.length + size == model.data.total) {
              noMore = true;
              page -- ;
            }
            size += model.data.records.length;
            records.addAll(model.data.records);
            er_controller.finishLoad(noMore: noMore);
          } else {
            size = model.data.records.length;
            records.assignAll(model.data.records);
            er_controller.finishRefresh();
            er_controller.resetLoadState();
          }

          if (size == 0) {
            isEmpty.value = true;
          } else {
            isEmpty.value = false;
          }
          records.refresh();
        });
      },
      onFailed: (err) async {
        page--;
        er_controller.finishRefresh();
      },
    );
  }

  jumpToDetail(String mallOrderNo, String mallOrderStatus) {
      Get.toNamed(Routes.ORDER_DETAIL, arguments: [mallOrderNo, mallOrderStatus, ToOrderDetailSource.mineTo]);
  }
}
